<?php

namespace IDock\Insurance\MACN\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Printer\ProposalStatusPrinter;

class ProposalStatusResponse extends AllMACNResponse
{
    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = iconv('UTF-8','GBK',$body);
        if ($body) {
            libxml_disable_entity_loader();
            $body = simplexml_load_string($body);
            $body = $this->objectToArray($body);
            $errorCode = $body['head']['statusCode'] ?? '';
            $jqErr = $body['body']['baseInfo']['efcHandleText'] ?? '';
            $ciErr = $body['body']['baseInfo']['bizHandleText'] ?? '';
            if (!in_array($errorCode,['SUCCESS','000000'])) {
                $err = $body['head']['description'] ?? '未知错误或无错误提示 ';
                if(!empty($jqErr)){
                    $err .= $jqErr;
                }
                if(!empty($ciErr)){
                    $err .= $ciErr;
                }
                $this->setError($err);
                return  $this;
            }

            $this->setBody($body['body'] ?? '');
        }

        return $this;
    }
    public function printerInit(): Response
    {
        $proposalStatusPrinter = new ProposalStatusPrinter();
        $body = $this->getBody();
        $biStatus = $body['baseInfo']['bizUnderWriteInd'] ?? '1';
        $ciStatus = $body['baseInfo']['efcUnderWriteInd'] ?? '1';
        $biCode = $this->getStatus($biStatus);
        $ciCode = $this->getStatus($ciStatus);
        $tips = '';
        $jqErr = $body['baseInfo']['efcHandleText'] ?? '';
        $ciErr = $body['baseInfo']['bizHandleText'] ?? '';
        if(!empty($jqErr)){
            $tips = $jqErr;
        }
        if(!empty($ciErr)){
            $tips .= $ciErr;
        }
        if($biCode == 'U1' && $ciCode == 'U1'){
            $proposalStatusPrinter->statusCode = 'U1';//核保通过
        }else if($biCode == 'U2' or $ciCode == 'U2'){
            $proposalStatusPrinter->statusCode = 'U2';//退回修改
            $proposalStatusPrinter->messageTip = $tips;
        }else if($biCode == 'U3' or $ciCode == 'U3'){
            $proposalStatusPrinter->statusCode = 'U3';//人工核保
            $proposalStatusPrinter->messageTip = $tips;
        }else if($biCode == 'U6' or $ciCode == 'U6'){
            $proposalStatusPrinter->statusCode = 'U6';//拒保
        }else if($biCode == 'U5' or $ciCode == 'U5'){
            $proposalStatusPrinter->statusCode = 'U5';//已出保单
        }else if($biCode == 'U4' or $ciCode == 'U4'){
            $proposalStatusPrinter->statusCode = 'U4';//复核失败
        }
        $proposalStatusPrinter->argc['payUrl'] =  $body['baseInfo']['h5Url'] ?? '';
        $proposalStatusPrinter->status = IDockEnums::$underStats[$proposalStatusPrinter->statusCode] ?? '';

        $this->printer = $proposalStatusPrinter;

        return $this;
    }

    private function getStatus($status)
    {
        $code = '';
        if(in_array($status,['1','3','5'])){
            $code = 'U1';//核保通过
        }else if($status == '2' or $status == 'B'){
            $code = 'U2';//退回修改
        }else if(in_array($status,['A','90','91','92','93','94','95','96','C','8','9'])){
            $code = 'U3';//人工核保
        }else if($status == '4'){
            $code = 'U6';//拒保
        }else if($status == '6'){
            $code = 'U5';//已出保单
        }else if($status == '7'){
            $code = 'U4';//复核失败
        }
        return $code;
    }
}
